package com.dpwx.framework.mvc;

import java.util.Stack;

import com.dpwx.framework.task.ITask;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.widget.Toast;

public abstract class BaseActivity extends Activity {
	
	private UiEventDispatcher mUiEventDispatcher;
	
	private BaseFacade mFacade;
	
	private Stack<BaseView> mViewStack;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.mFacade = onInitFacade();
		mViewStack = new Stack<BaseView>();
	}
	
	private void showView(BaseView view) {
		if (mViewStack.contains(view)) {
			BaseView oldView = null;
			do {
				oldView = mViewStack.pop();
			} while (oldView != view);
		}
		mViewStack.push(view);
		if (view.getViewObject() != null) {
			this.setContentView(view.getViewObject());
		} else if (view.getResId() > 0) {
			this.setContentView(view.getResId());
		} else {
			throw new RuntimeException("invalid BaseView object");
		}
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		if (mViewStack.size() < 1) {
			mFacade.startup(getIntent());
		}
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		mFacade.onDestroy();
	}
	
	@Override
	public void onBackPressed() {
		if (mViewStack.size() > 1) {
			mViewStack.pop();
			showView(mViewStack.peek());
		} else {
			super.onBackPressed();
		}
	}
	
	
	protected abstract BaseFacade onInitFacade();
	
	public void clearViewStack() {
		mUiEventDispatcher.doTask(new ITask() {
			@Override
			public void doTask() {
				mViewStack.clear();			
			}
		});
	}
	
	
	public UiEventDispatcher getUiEventDispatcher() {
		if (mUiEventDispatcher == null) {
			mUiEventDispatcher = new UiEventDispatcher();
		}
		return mUiEventDispatcher;
	}
	
	public class UiEventDispatcher extends Handler{
		
		public static final int MSG_SHOW_VIEW = 1;
		public static final int MSG_SHOW_TOAST = 2;
		public static final int MSG_DO_TASK = 3;
		
		UiEventDispatcher() {
			super(Looper.getMainLooper());
		}
		
		public void showView(BaseView view) {
			sendMessage(obtainMessage(MSG_SHOW_VIEW, view));
		}
		
		public void showToast(String tip) {
			sendMessage(obtainMessage(MSG_SHOW_TOAST, tip));
		}
		
		public void doTask(ITask task) {
			sendMessage(obtainMessage(MSG_DO_TASK, task));
		}
		
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case MSG_SHOW_VIEW:
				BaseView view = (BaseView) msg.obj;
				BaseActivity.this.showView(view);
				break;
			case MSG_SHOW_TOAST:
				Toast.makeText(BaseActivity.this, (String)msg.obj, Toast.LENGTH_SHORT).show();
				break;
			case MSG_DO_TASK:
				ITask task = (ITask) msg.obj;
				task.doTask();
				break;
			default:
				break;
			}
		}

	}

}
